# Makefile for bounds_check library

CC = gcc
CFLAGS = -Wall -Wextra -std=c11 -O2 -I./include
TARGET = libbounds_check.a
TEST_TARGET = test/test_bounds_check
EXAMPLE_TARGET = example

# Source files
SRCS = src/common.c \
       src/strcpy_s.c \
       src/strncpy_s.c \
       src/strcat_s.c \
       src/strncat_s.c \
       src/memcpy_s.c \
       src/memmove_s.c \
       src/memset_s.c \
       src/strnlen_s.c \
       src/gets_s.c \
       src/check_buffer.c

OBJS = $(SRCS:.c=.o)

.PHONY: all clean test example install uninstall

all: $(TARGET)

$(TARGET): $(OBJS)
	ar rcs $@ $^

%.o: %.c
	$(CC) $(CFLAGS) -c $< -o $@

test: $(TEST_TARGET)
	./$(TEST_TARGET)

$(TEST_TARGET): test/test_bounds_check.c $(TARGET)
	$(CC) $(CFLAGS) -o $@ test/test_bounds_check.c -L. -lbounds_check

example: example.c $(TARGET)
	$(CC) $(CFLAGS) -o $(EXAMPLE_TARGET) example.c -L. -lbounds_check
	./$(EXAMPLE_TARGET)

clean:
	rm -f $(OBJS) $(TARGET) $(TEST_TARGET) $(EXAMPLE_TARGET)

install: $(TARGET)
	cp $(TARGET) /usr/local/lib/
	cp include/bounds_check.h /usr/local/include/

uninstall:
	rm -f /usr/local/lib/$(TARGET)
	rm -f /usr/local/include/bounds_check.h